package thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author hubert
 * @date 2018/10/10
 * @description ThreadTest3
 */
public class ThreadTest3 {

	public static void main(String[] args) {
		ExecutorService pool = Executors.newFixedThreadPool(10);

		List<Future> futures = new ArrayList<>(10);
		List<FutureTask> futureTasks = new ArrayList<>(10);
		for (int i = 0; i <10 ; i++) {
			MyCallable mc = new MyCallable("mc"+(0+i));
			FutureTask<Boolean> futureTask = new FutureTask<>(mc);
			pool.execute(futureTask);
			futureTasks.add(futureTask);
			//Future future = pool.submit(mc);
			//futures.add(future);
		}
		// 将Callable写的任务封装到一个由执行者调度的FutureTask对象
//		MyCallable mcTest = new MyCallable("test");
//		FutureTask<Boolean> futureTask1 = new FutureTask<Boolean>(mcTest);
//		pool.execute(futureTask1);

		for (int i = 0; i < futureTasks.size(); i++) {
			FutureTask<Boolean> result = futureTasks.get(i);
			try {
				System.out.println(result.get(1, TimeUnit.SECONDS));
			} catch (InterruptedException e) {
				System.out.println(e.getMessage()+e);
			} catch (ExecutionException e) {
				System.out.println(e.getMessage()+e);
			} catch (TimeoutException e) {
				e.printStackTrace();
			}
		}

		/*try {
			for (int i = 0; i < futures.size(); i++) {
				boolean flag = false;
				try {
					//阻塞job主线程，防止job重复提交线程
					Future future = futures.get(i);
					//System.out.println(future.isDone()+","+future.toString());
					//10分钟超时,超时线程仍然在执行
					flag = (boolean)future.get(1, TimeUnit.SECONDS);
				}catch (Exception e) {
					System.err.println("进程超时："+e.getMessage()+","+e);
				}
					System.out.println("Thread"+(1+i)+":"+flag);
				}
		} catch (Exception e) {
			System.err.println("进程超时异常;"+e);
		}*/



	}
}
